﻿using Knight.Models.Entity;
using Knight.Models.Services;
using Knight.Services;
using Knight.Web.Infrastructure.Menus;
using Knight.Web.Infrastructure.Mvc;
using Knlght.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Areas.Manage.Controllers.Filters;

namespace WebApp.Areas.Manage.Controllers
{
    public class SinglePageController : BaseAdminController
    {
        [MallAdminMenu("SinglePage_Info")]
        [Permission("SinglePage_Info")]
        public ActionResult Index()
        {
            var service = getDefaultService();          
            var rsp = service.GetList<SinglePageInfo>(null, o=>new SinglePageInfo() { Id=o.Id,Title=o.Title});
            if (rsp.IsSuccess)
            {
                ViewBag.TypeList = rsp.Models;
            }
            return View();
        }

        [HttpPost]
        public JsonResult Paging(PagingParameter parameter)
        {
            var query = new QueryRequest<SinglePageInfo>();
            if (parameter.Query.CreateCriterionByString(nameof(SinglePageInfo.Title)) != null)
            {
                var username = parameter.Query.CreateCriterionByString(nameof(SinglePageInfo.Title)).Value.ToString();
                query.Add(o => o.Title.Contains(username));
            }
            if (parameter.Query.CreateCriterionByBoolean(nameof(SinglePageInfo.IsEnabled)) != null)
            {
                var isEnabled = Convert.ToBoolean(parameter.Query.CreateCriterionByBoolean(nameof(SinglePageInfo.IsEnabled)).Value);
                query.Add(o => o.IsEnabled == isEnabled);
            }           
            query.AddOrder(nameof(SinglePageInfo.Title), true);
            query.AddOrder(nameof(SinglePageInfo.AddTime), true);
            var pagingRequest = new PagingRequest<SinglePageInfo>
            {
                PageIndex = parameter.PageIndex,
                PageSize = parameter.PageSize,
                Query = query
            };
            var service = getDefaultService();
            var rsp = service.Paging(pagingRequest);
            return EJPagingJson(rsp);
        }

        [HttpPost]
        public JsonResult Add(SinglePageInfo model)
        {
            var service = new SinglePageInfoService();
            var rsp = service.Add(model);
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Update(SinglePageInfo model)
        {
            var service = getDefaultService();
            var rsp = service.Update(model);
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Updates(int[] ids, string property, object value)
        {
            var service = getDefaultService();
            var rsp = service.UpdateToList<SinglePageInfo>(o => ids.Contains(o.Id), i => new SinglePageInfo { IsEnabled = (bool)value });
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Remove(int id)
        {
            var service = getDefaultService();
            var rsp = service.Remove<SinglePageInfo>(o=>o.Id==id);
            return EJActionJson(rsp);
        }
        [HttpPost]
        public JsonResult Removes(int[] ids)
        {
            var service = getDefaultService();
            var rsp = service.Remove<SinglePageInfo>(o => ids.Contains(o.Id));
            return EJActionJson(rsp);
        }
    }
}